package com.cyq.day12;

import java.util.Scanner;

public class ATMSystem {
    static ATM[] atm = new ATM[5];
    public  static ATM[] first(){
        atm[0]=new ATM();
        atm[0].userName="aaaaaa";
        atm[0].passWord="111111";
        atm[0].cash=1000;
        atm[1]=new ATM();
        atm[1].userName="bbbbbb";
        atm[1].passWord="222222";
        atm[1].cash=2000;
        atm[2]=new ATM();
        atm[2].userName="cccccc";
        atm[2].passWord="333333";
        atm[2].cash=3000;
        atm[3]=new ATM();
        atm[3].userName="dddddd";
        atm[3].passWord="444444";
        atm[3].cash=4000;
        return atm;
    }
    public static void process(){
        int loginReturn = login();
        while (true) {
            Scanner scanner = new Scanner(System.in);
            menu();
            int userInput = scanner.nextInt();
            switch (userInput) {
                case 1:
                    save(loginReturn);
                    break;
                case 2:
                    get(loginReturn);
                    break;
                case 3:
                    search(loginReturn);
                    break;
                case 4:
                    transfer(loginReturn);
                    break;
                case 5:
                    System.exit(0);
                case 6:
                    login();
                    break;
                case 7:
                    changePwd(loginReturn);
                    break;
                default:
                    System.out.println("输入有误");
                    break;
            }
        }
    }
    public static void main(String[] args) {
        first();
        process();
    }
    public static int login(){
        int count = 0;
        outer:for (int i = 1; i <= 3; i++) {
            System.out.println("请输入账号:");// 提示用户输入账户
            Scanner userSc = new Scanner(System.in);
            String userIn = userSc.next();
            System.out.println("请输入密码:");// 提示用户输入密码
            Scanner passwordSc = new Scanner(System.in);
            String passwordIn = passwordSc.next();
            for (int j = 0; j <atm.length  ; j++) {// 判断输入账户和密码是否匹配数据库
                if ( (atm[i].userName).equals(userIn) && (atm[i].passWord).equals(passwordIn)) {
                    count = j;// 匹配成功，将当前账户在数据库中的账户组的下标值赋值给count
                    break outer;
                } else if (i == 1 && j == atm.length - 1) {// 第一次输入错误
                    System.out.println("账号或密码错误，请重新输入！");
                } else if (i == 2 && j == atm.length - 1) {// 第二次输入错误
                    System.out.println("账号或密码错误，请重新输入！");
                    System.out.println("这是您的最后一次输入机会，若账号或密码错误，将锁定您的银行卡");
                } else if (j == atm.length - 1) {// 第三次输入错误
                    System.out.println("您的错误次数超过限制，请联系相关银行！");
                    System.exit(0);// 三次错误后，程序终止
                }
            }
        }

        return count;
    }
    public static void menu(){
        System.out.println("请选择：1-存款 2-取款 3-查询 4-转账 5-退出 6-切换用户 7-修改密码");
    }
    public static void save(int count){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入想要存款的金额");
        int deposit = scanner.nextInt();
        atm[count].cash = atm[count].cash + deposit ;
        System.out.println("账户的余额为" +atm[count].cash);
    }
    public static void get(int count){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入想要取出的金额");
        int withDraw = scanner.nextInt();
        if (atm[count].cash>=withDraw) {
            atm[count].cash = atm[count].cash - withDraw ;
            System.out.println("账户的余额为" + atm[count].cash);
        }else {
            System.out.println("余额不足");
        }
    }
    public static void search(int count){
        System.out.println("查询账户的余额为" +  atm[count].cash);
    }
    public static void transfer(int count){
        Scanner scanner = new Scanner(System.in);
        boolean isExist=false;
        System.out.println("请输入想要转账的账户的用户名");
        String userId = scanner.next();
        if (userId.equals(atm[count].cash)){
            System.out.println("不能转给自己");
            return;
        }
        int tranSportID = 0;
        for (int i = 0; i <  atm.length; i++) {
            if (userId.equals(atm[i].userName)) {
                tranSportID = i;
                isExist=true;
            }
        }
        if (!isExist){
            System.out.println("你输入的账号不存在请重新输入");
            return;
        }
        System.out.println("请输入想要转入的金额");
        double tranSport = scanner.nextDouble();
        if (tranSport>=atm[count].cash){
            System.out.println("余额不足不能转账");
            return;
        }
        atm[count].cash = atm[count].cash - (int)tranSport ;
        atm[tranSportID].cash = atm[tranSportID].cash + (int)tranSport ;
        System.out.println("账户的余额为" +  atm[count].cash);
        System.out.println("转入账户的余额为" + atm[tranSportID].cash);
    }
    public static void changePwd(int count){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入想要修改的密码");
        String newPwd= scanner.next();
        atm[count].passWord=newPwd;

    }
}
